﻿using DDit.Component.Tools.weixin;
using DDit.Core.Data.Entity;
using DDit.Core.Data.Entity.CoreEntity;
using DDit.Core.Data.Repository.Repositories.CoreRepositories;
using DDitApplicationFrame.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DDitApplicationFrame.Areas.SystemSchema.Controllers
{
    public class WxUserController : BaseController
    {
        //
        // GET: /SystemSchema/WxUser/

        public ActionResult Index()
        {
            ViewBag.Group = WxRepository.GetGroupList();
            return View();
        }

        [HttpPost]
        public ActionResult GetUserList(FansUser model = null)
        {
            try
            {
                var ListModel = WxRepository.GetUserList(model);
                return this.JsonResult(ListModel);
                //var ListModel = WxUserManage.UserListTest();
                //return this.JsonResult(new Tuple<int, List<WxUser>>(total, ListModel));
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }

        }

        [HttpPost]
        public ActionResult AddorEditUserInfo(WxUser userModel)
        {
            try
            {
                WxUserManage.SetRemark(userModel.openid, userModel.remark);
                return Json(new ResultEntity { result = true });
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }

        }
        [HttpPost]
        public ActionResult PullBackUser(string openid)
        {
            try
            {
                WxUserManage.PullBlack(new List<string>() { openid });
                return Json(new ResultEntity { result = true });
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }
        }

        [HttpPost]
        public ActionResult CancelBackUser(string openid)
        {
            try
            {
                WxUserManage.CancelBlack(new List<string>() { openid });
                return Json(new ResultEntity { result = true });
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }
        }

        [HttpPost]
        public ActionResult BackUser(FansUser model = null)
        {
            try
            {
                var ListModel = WxRepository.GetBlackUserList(model);
                return this.JsonResult(ListModel);
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }
        }


        [HttpPost]
        public ActionResult AddorEditUserGroup(string openid, string oldTag, List<UserGroup> mplist)
        {
            try
            {
                WxRepository.SetUserGroup(new List<string>() { openid }, mplist, oldTag);
                return Json(new ResultEntity { result = true });
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }

        }
        public ActionResult Black()
        {
            return View();
        }

        public ActionResult Group()
        {
            return View();
        }

        public ActionResult GetGroupList(UserGroup item = null)
        {
            try
            {
                var ListModel = WxRepository.GetGroupList(item);
                return this.JsonResult(ListModel);
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }

        }

        public ActionResult AddorEditGroup(WxGroup item)
        {
            try
            {
                WxUserManage.SetTag(item.id, item.name);
                ViewBag.Group = WxRepository.GetGroupList();
                return Json(new ResultEntity { result = true });
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }
        }

        [HttpPost]
        public ActionResult RemoveGroup(int id)
        {
            try
            {
                WxUserManage.DelTag(id);
                ViewBag.Group = WxRepository.GetGroupList();
                return Json(new ResultEntity { result = true });
            }
            catch (Exception ex)
            {
                return Json(new ResultEntity { result = false, message = ex.Message });
            }
        }
    }
}
